package com.zfzs.post.modules.common.service.impl;

import com.zfzs.post.enums.TeamType;
import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.modules.common.domain.AgentContact;
import com.zfzs.post.modules.common.repository.AgentContactRepository;
import com.zfzs.post.modules.common.service.AgentContactService;
import com.zfzs.post.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 查询合伙人信息
 *
 * @author: ck
 * @create: 2019/8/20/15:16
 **/
@Service
public class AgentContactServiceImpl implements AgentContactService {

    @Autowired
    private AgentContactRepository agentContactRepository;

    @Override
    public AgentContact findOne(Long id) {
        Optional<AgentContact> p = agentContactRepository.findById(id);
        if (!p.isPresent()) {
            return null;
        }
        return p.get();
    }

    @Override
    public void updateContact(Long agentId, Long pidId,Long channelId) {
        //增加代理商
        //增加上级代理商关联
        AgentContact ag=new AgentContact();
        ag.setAgentId(agentId);
        ag.setLevel(0);
        ag.setChildId(agentId);
        ag.setChannelId(channelId);
        agentContactRepository.save(ag);

        List<Long> idList = findAllParentIdList(pidId,channelId);

        AgentContact agentContact;
        for (int i = 0; i < idList.size(); i++) {
            Long aLong =  idList.get(i);
            agentContact=new AgentContact();
            agentContact.setChildId(agentId);
            agentContact.setLevel(i+1);
            agentContact.setAgentId(aLong);
            agentContact.setChannelId(channelId);
            agentContactRepository.save(agentContact);
        }

    }

    @Override
    public AgentContact save(AgentContact agentContact) {
        return agentContactRepository.save(agentContact);
    }

    @Override
    public List<AgentContact> findAllParent(Long agentId,Long channelId){
        return agentContactRepository.findByChildIdAndChannelId(agentId,channelId);
    }


    @Override
    public List<Long> findAllParentIdList(Long agentId,Long channelId){
        return findAllParent(agentId,channelId).stream().map(AgentContact::getAgentId).collect(Collectors.toList());
    }



    @Override
    public List<AgentContact> queryAll() {
        return agentContactRepository.findAll();
    }

    @Override
    public List<Long> findAllTeamIdsExceptSelf(Long agentId,Long channelId) {
        List<AgentContact> list = agentContactRepository.findByAgentIdAndChannelIdAndLevelNot(agentId,channelId ,0);
        List<Long> idList = list.stream().map(AgentContact::getChildId).collect(Collectors.toList());
        if(idList.isEmpty()){
            idList.add(0L);
        }
        return idList;
    }

    @Override
    public List<Long> findAllTeamIds(Long agentId,Long channelId) {
        List<AgentContact> ids = findByAgentId(agentId,channelId);
        return ids.stream().map(AgentContact::getChildId).collect(Collectors.toList());
    }


    @Override
    public List<AgentContact> findByAgentId(Long agentId,Long channelId) {
        return agentContactRepository.findByAgentIdAndChannelId(agentId,channelId);
    }


    @Override
    public List<Long> findDirectlyIds(Long agentId,Long channelId) {
        List<AgentContact> list = agentContactRepository.findByAgentIdAndChannelIdAndLevel(agentId,channelId,1);
        List<Long> idList = list.stream().map(AgentContact::getChildId).collect(Collectors.toList());
        if(idList.isEmpty()){
            idList.add(0L);
        }
        return idList;
    }

    @Override
    public List<Long> findPartnerTeam(Long agentId,Long channelId) {
        List<AgentContact> list = agentContactRepository.findByAgentIdAndChannelIdAndLevelGreaterThan(agentId, channelId,1);
        List<Long> idList =   list.stream().map(AgentContact::getChildId).collect(Collectors.toList());
        if(idList.isEmpty()){
            idList.add(0L);
        }
        return idList;
    }

    @Override
    public List<Long> findListByTeamType(String teamType, Long agentId,Long channelId) {
        List<Long> teamList;
        if (StringUtils.isEmpty(teamType) || TeamType.TEAM.equalsIgnoreCase(teamType)) {
            teamList = findAllTeamIdsExceptSelf(agentId,channelId);
        } else if (TeamType.DIRECTLY.equalsIgnoreCase(teamType)) {
            teamList = findDirectlyIds(agentId,channelId);
        } else if (TeamType.PARTNER.equalsIgnoreCase(teamType)) {
            teamList = findPartnerTeam(agentId,channelId);
        } else {
            throw new BadRequestException("类型不正确");
        }
        return teamList;

    }
}
